package com.nulldev.util.web.HttpServer3.legacyPages.v2;

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

import com.nulldev.util.JVM.JVM;
import com.nulldev.util.data.Sets.DataSet;
import com.nulldev.util.web.HttpServer3.Page;
import com.nulldev.util.web.HttpServer3.legacyPages.v1.HttpPage;
import com.nulldev.util.web.HttpServer3.legacyPages.v1.RequestType;
import com.nulldev.util.web.URLUtils.URLQueryParser;
import com.nulldev.util.web.URLUtils.nStreamHandlerFactory;

/**
 * Legacy HttpPage2. <br>
 * This should only be used for compatibility purposes.
 * 
 * @author null
 * @since 2.0
 */
public abstract class HttpPage2 implements Page {
	private volatile String id;

	public HttpPage2(String id) {
		if (!id.startsWith("/")) {
			id = "/" + id;
		}
		this.id = id.replaceAll(" ", "%20");
	}

	public byte[] toBytes(final String str) {
		return str.getBytes();
	}

	public String getID() {
		return this.id;
	}

	private String mime = null;

	/**
	 * Gets a response.
	 * 
	 * @returns DataSet[ResponseCode,byte[] data]
	 */
	public abstract DataSet<Integer, InputStream> request(final RequestType type, final String url, final HashMap<String, String> headers,
			final byte[] extraData, final String ip); // NOW ACTUALLY COMPATIBLE WITH OLD CODE

	public String getMimeType() {
		return this.mime;
	}

	public void setMimeType(final String mime) {
		this.mime = mime;
	}

	public HttpPage2 _setMimeType(final String mime) {
		this.mime = mime;
		return this;
	}

	public static HttpPage2 convert(final HttpPage page) {
		final HttpPage2 htp = new HttpPage2(page.getID()) {

			@Override
			public DataSet<Integer, InputStream> request(RequestType type, String url, final HashMap<String, String> headers, byte[] extraData,
					final String ip) {
				if (page.isOverBytes()) {
					return new DataSet<Integer, InputStream>(200, new ByteArrayInputStream(page.handleRequest_Alt(url, ip, type)));
				} else if (page.sendRaw()) {
					try {
						return new DataSet<Integer, InputStream>(200, new FileInputStream(page.getPath()));
					} catch (Exception e) {
						JVM.errorln(e);
					}
				}
				return new DataSet<Integer, InputStream>(200, new ByteArrayInputStream(toBytes(page.handleRequest(url, ip, type))));
			}
		};
		if (page.isCustomMime()) {
			htp.setMimeType(page.getMime());
		}
		return htp;
	}

	public InputStream blankStream() {
		return nStreamHandlerFactory.BLANK;
	}

	public InputStream createStream(final String text) {
		return new ByteArrayInputStream(text.getBytes());
	}

	public InputStream createStream(final byte[] data) {
		return new ByteArrayInputStream(data);
	}

	public Map<String, String> parseHeaders(final String url) {
		if (url == null || url.isEmpty()) {
			return new TreeMap<String, String>();
		} else
			return URLQueryParser.extract(url); // TODO: Uhhhh, this doesn't...seem...right???
	}

	public boolean allowRanges() {
		return true;
	}
}
